{-
Module      : Strategy.Android.Util
Description : Provides Utilities For Analyzing Android Projects Built Using Android Studio

Typical android project built by android studio includes following android plugins (placed in build.gradle):
- 'com.android.application'
- 'kotlin-android'

And in your top-level build.gradle:
- classpath 'com.android.tools.build:gradle:7.0.0'
- classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.22"

These plugins introduce gradle configurations for development and testing. This module,
provides utilities to infer if a configuration is part of default configuration generated by aforementioned plugins.

References:
- https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependency_management
- https://developer.android.com/studio/releases/gradle-plugin
- https://github.com/JetBrains/kotlin/tree/f37d8809643ceab07fb8e3211c00f3d98e767f60/libraries/tools/kotlin-gradle-plugin
-}

module Strategy.Android.Util (
  isDefaultAndroidDevConfig,
  isDefaultAndroidTestConfig,
) where

import Data.Text (Text)
import Data.Text qualified as Text

-- Since this introduces somewhat drastic change, to stay on side of caution,
-- intentionally performing exact matches, as opposed to prefix/infix matches.

isDefaultAndroidDevConfig :: Text -> Bool
isDefaultAndroidDevConfig config = config `elem` androidDefaultConfigs
  where
    androidDefaultConfigs =
      [ "lintChecks"
      , "lintClassPath"
      , "lintPublish"
      , -- For 'debug' sources
        "debugApiDependenciesMetadata"
      , "debugCompileClasspath"
      , "debugCompileOnly"
      , "debugCompileOnlyDependenciesMetadata"
      , "debugImplementationDependenciesMetadata"
      , "debugIntransitiveDependenciesMetadata"
      , "debugReverseMetadataValues"
      , "debugRuntimeClasspath"
      , "debugRuntimeOnlyDependenciesMetadata"
      , -- For Release Compiling
        "compileOnlyDependenciesMetadata"
      , "releaseCompileOnly"
      , "releaseCompileOnlyDependenciesMetadata"
      , -- Bundling
        "debugWearBundling"
      , -- Annotations
        "debugAnnotationProcessorClasspath"
      , "releaseAnnotationProcessorClasspath"
      , -- Configuration providing JDK image for compiling Java 9+ sources
        "androidJdkImage"
      , -- Misc
        "kotlinCompilerClasspath"
      , "kotlinCompilerPluginClasspathDebug"
      , "kotlinCompilerPluginClasspathDebugAndroidTest"
      , "kotlinCompilerPluginClasspathDebugUnitTest"
      , "kotlinCompilerPluginClasspathReleaseUnitTest"
      , "kotlinCompilerPluginClasspathRelease"
      , "kotlinKlibCommonizerClasspath"
      , "kotlinNativeCompilerPluginClasspath"
      ]

isDefaultAndroidTestConfig :: Text -> Bool
isDefaultAndroidTestConfig config =
  any (config `isPrefixedBy`) ["androidTest", "debugAndroidTest", "releaseUnitTest", "debugUnitTest"]
    || (config `elem` androidDefaultConfigs)
  where
    isPrefixedBy :: Text -> Text -> Bool
    isPrefixedBy = flip Text.isPrefixOf

    androidDefaultConfigs =
      [ -- The Jacoco agent to use to get coverage data.
        "androidJacocoAnt"
      , -- Metadata for test dependencies
        "testApiDependenciesMetadata"
      , "testCompileOnlyDependenciesMetadata"
      , -- For 'testDebug' sources
        "testDebugApiDependenciesMetadata"
      , "testDebugCompileOnlyDependenciesMetadata"
      , "testDebugImplementationDependenciesMetadata"
      , "testDebugIntransitiveDependenciesMetadata"
      , "testDebugRuntimeOnlyDependenciesMetadata"
      , -- testImplementation metadata
        "testImplementationDependenciesMetadata"
      , "testIntransitiveDependenciesMetadata"
      , -- For 'testRelease' sources
        "testReleaseApiDependenciesMetadata"
      , "testReleaseCompileOnlyDependenciesMetadata"
      , "testReleaseImplementationDependenciesMetadata"
      , "testReleaseIntransitiveDependenciesMetadata"
      , "testReleaseRuntimeOnlyDependenciesMetadata"
      , "testRuntimeOnlyDependenciesMetadata"
      ]
